﻿using JStore.Core.Entities;
using SpeadFrm.MvcPager;
using SpeadFrm.ORM;

namespace JStore.Data.Daos
{
    /// <summary>
    ///
    /// </summary>
    public class SysMsgDao : BaseDao<SysMsg>
    {
        public PagedList<SysMsg> GetPagedList(string userName, int pageIndex, int pageSize)
        {
            var sql = Sql.Builder.Where("1=1");
            if (!string.IsNullOrEmpty(userName))
                sql.Where("UserName = @0 AND IsDelete = 0", userName);
            sql.OrderBy("CreateDate DESC");
            return PagedList(pageIndex, pageSize, sql);
        }

        public SysMsg GetById(int id)
        {
            var sql = Sql.Builder.Where("Id = @0", id);
            return FirstOrDefault(sql);
        }

        public void SetRead(int id)
        {
            var sql = Sql.Builder
                .Append("UPDATE SysMsg")
                .Append("SET IsRead = 1")
                .Where("Id = @0", id);
            Execute(sql);
        }

        public int GetCount(string userName)
        {
            var sql = Sql.Builder
                .Select("COUNT(1)")
                .From("SysMsg")
                .Where("UserName = @0 AND IsDelete = 0 AND IsRead = 0", userName);
            return FirstOrDefault<int>(sql);
        }
    }
}

